<?php
defined('IN_PHPCMS') or exit('No permission resources.');
define('CACHE_MODEL_PATH',CACHE_PATH.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);
pc_base::load_app_class('admin', 'admin', 0);
pc_base::load_sys_class('form','',0);
class mood_ma extends admin {
	
	public function __construct() {
		parent::__construct();
	}
	
	//魔道列表
	public function init() {
		$modao_db = pc_base::load_model('mood_ma_model');
		$id = isset($_GET['id']) &&  intval($_GET['id']) ? intval($_GET['id']) : '';
		$img_type = isset($_GET['img_type']) &&  intval($_GET['img_type']) ? intval($_GET['img_type']) : 0;
		$order = isset($_GET['order']) &&  intval($_GET['order']) ? intval($_GET['order']) : 0;
		$sql = '1=1';
		if(!empty($_REQUEST['type'])){
			$sql .= " and img_type = '{$_REQUEST['type']}'";
		}
		$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
		$sql_order = " `id` desc";
		$data = $modao_db->listinfo($sql, $sql_order, $page);
		$pages = $modao_db->pages;
		pc_base::load_sys_class('form', '', 0);
		include $this->admin_tpl('modao_list');
	}
	
	public function add(){
		$modao_db = pc_base::load_model('mood_ma_model');
		$machine_image_db = pc_base::load_model('machine_image_model');
		require CACHE_MODEL_PATH.'content_form.class.php';
		$modelid = "3";
		$catid = 8;
		$content_form = new content_form($modelid,$catid);
		$forminfos = $content_form->get();
		if($_POST){
			$id = $modao_db->insert(
							array(
								'name'=>$_POST['info']['title'],
								'img_type'=>$_POST['img_type'],
								'addtime'=>time()
							),true);
			//ar_dump($_POST['pictureurls_url']);exit;							
			foreach($_POST['pictureurls_url'] as $v){
				$machine_image_db->insert(array('img_id'=>$id,'url'=>$v));
			}
			if($id){
				showmessage(L('operation_success'), '?m=mood_ma&c=mood_ma&a=init');
			}else{
				showmessage(L('operation_failure'), HTTP_REFERER);
			}				
		}
		include $this->admin_tpl('modao_form');
	}
	
	public function listorder(){
		$modao_db = pc_base::load_model('mood_ma_model');
		if(isset($_POST['dosubmit'])) {
			foreach($_POST['listorder'] as $id => $order) {
				$id = intval($id);
				$order = intval($order);
				$modao_db->update(array('listorder'=>$order), array('id'=>$id));
			}
			showmessage(L('operation_success'), HTTP_REFERER);
		} else {
			showmessage(L('please_in_admin'), HTTP_REFERER);
		}
	}
	
	public function del(){
		$modao_db = pc_base::load_model('mood_ma_model');
		if((!isset($_GET['id']) || empty($_GET['id'])) && (!isset($_POST['id']) || empty($_POST['id'])) && !$id) {
			showmessage(L('illegal_action'), HTTP_REFERER);
		}
		if(is_array($_POST['id'])) {
			foreach($_POST['id'] as $id){
				$modao_db->delete(array('id'=>$id));
			}
			showmessage(L('operation_success'), HTTP_REFERER);
		} 
	}
}